package ldh.im.client.interceptor;

import ldh.im.client.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ErrorHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandler.class);

    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ResponseResult exceptionHandler(HttpServletRequest req, Exception e) throws Exception {
        LOGGER.error("error", e.getMessage());
        return ResponseResult.build().error(-100, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult exceptionHandler(Exception e) {
        return ResponseResult.build().error(500, e.getMessage());
    }
}
